package minitfull

import (
	"gitee.com/dennis-mxx/gcode-common/menv"
	"gitee.com/dennis-mxx/gcode-common/mlogger"
	"gitee.com/dennis-mxx/gcode-data/mredis"
	"gitee.com/dennis-mxx/gcode-starter/minit"
	"gitee.com/dennis-mxx/gcode-starter/minitmysql"
	"gitee.com/dennis-mxx/gcode-starter/minitredis"
	"gitee.com/dennis-mxx/gcode-starter/minitweb"
	"gitee.com/dennis-mxx/gcode-web/mweb"
	"github.com/gorilla/sessions"
	"strings"
)

func InitializeFile(defEnv string) {
	logger := minit.InitializeFile(defEnv)
	Complete(logger)
}

func Initialize(loader menv.ConfigRender, defEnv string, dest ...any) {
	logger := minit.Initialize(loader, defEnv, dest)
	Complete(logger)
}

func Complete(logger *mlogger.TraceLogger) {
	environment := menv.Environment

	if environment.Redis != nil && environment.Redis.Addr != "" {
		if environment.Web != nil && environment.Web.Session == "redis" {
			mweb.RegisterSessionOptions("redis", func(ginOption *menv.Web) sessions.Store {
				prefix := strings.ReplaceAll(menv.Environment.ServerName, "-", ":")
				return mweb.NewRedisStore(mredis.RedisClient, prefix, []byte(menv.Environment.Web.SessionSecret))
			})
		}
	}
	minit.Complete(logger, minitmysql.LoaderMysql, minitredis.LoaderRedis, minitweb.LoadWeb)
}
